﻿using Layans.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Api.Models;
using System.Web.Mvc;

using WebMatrix.WebData;
namespace Api.Controllers
{
    public class UserPostController : ApiController
    {
        LlaansContext db = new LlaansContext();

        public UsersPost DoCreate(UsersPost userPost)
        {
            var post = new UsersPost
            {

                Posts = userPost.Posts,
                avatars = Avatarss,
                UserName = userName,
                DateTime = DateTime.Now,

            };

            return UsersPosts;
        }

        public UsersPost ViewPost(int postId)
        {
            if (!db.UserPostVotes.Any(x => x.Id == postId))
            {
               
            }


            var post = db.UsersPosts.Where(x => x.Id == postId)
                .Select(x => new FullUserPostViewModel
                {
                    Avatar = x.avatars,
                    Post = x.Posts,
                    UserName = x.UserNamees.UserName,
                    CreationTime = x.DateTime,
                }).SingleOrDefault();

            post.Comment = db.usersPostComments.Where(x => x.PostComment.Id == postId)
                .Select(y => new UserPostAnswerViewModel
                {
                    comment = y.Comment,
                    Avatar = y.Avatar.avatar,
                    CreationTime = y.DateTime,
                    Name = y.UserName.UserName,
                });

            return Post(post);
        }

        private UsersPost Post(FullUserPostViewModel post)
        {
            throw new NotImplementedException();
        }

        public UsersPostComment DoAnswer(int userpostId, string comment)
        {
            var userPost = db.UsersPosts.FirstOrDefault(x => x.Id == userpostId);
            db.UsersPostComments.Add(
                new UsersPostComment()
                {
                    Avatar = Avatars,
                    Comment = comment,
                    DateTime = DateTime.Now,
                    UserName = userName,
                });
            db.SaveChanges();
            return userPos;
        }

        public UserPostVote Vote(int id)
        {
            if (db.UserPostVotes.Any(x => x.Posts.Id == id && x.Name.UserId == WebSecurity.CurrentUserId))
            {
                var currentEntry = db.UserPostVotes.SingleOrDefault
                    (x => x.Posts.Id == id && x.Name.UserId == WebSecurity.CurrentUserId);
                currentEntry.VotedBy = true;
                db.SaveChanges();
            }

            else
            {
                var user = db.Author.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
                var userPost = db.Pics.FirstOrDefault(x => x.Id == id);
                db.UserPicVote.Add(new UserPicVote()
                {
                    Post = userPost,
                    User = user,
                    UpVote = true,
                });

                db.SaveChanges();
            }

            int rating = db.UserPicVote.Count(y => y.Post.Id == id && y.UpVote);

            return Contentss(rating.ToString());
        }

        private UserPostVote Contentss(string p)
        {
            throw new NotImplementedException();
        }

        public string Avatarss { get; set; }

        public LForum.Models.ExternalLoginConfirmationViewModel userName { get; set; }

        public ActionResult Redirects { get; set; }

        public ActionResult userPosts { get; set; }

        public UsersPost UsersPosts { get; set; }

        public Avatar Avatars { get; set; }

        public UsersPost userPostss { get; set; }

        public UsersPostComment userPos { get; set; }
    }
}
